package logic

import (
	"2210a/practice/models/mysql"
	"context"
	"fmt"

	"2210a/practice/practicerpc/internal/svc"
	"2210a/practice/practicerpc/practicerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *practicerpc.ShopAddRequest) (*practicerpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line
	var login mysql.Product
	mysql.Db.Debug().Where("user_id = ?", in.UserId).Find(&login)
	if login.ID == 0 {
		return nil, fmt.Errorf("当前用户没注册，请重新注册")
	}
	product := mysql.Product{
		UserId:         in.UserId,
		Avatar:         in.Avatar,
		ShopName:       in.ShopName,
		ShopNavigation: in.ShopNavigation,
		ShopLabel:      in.ShopLabel,
		ShopBank:       in.ShopBank,
		SalesVolume:    in.SalesVolume,
		Status:         in.Status,
	}
	mysql.Db.Debug().Create(&product)
	return &practicerpc.ShopAddResponse{Id: int64(product.ID)}, nil
}
